package controllers;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.tanesha.recaptcha.ReCaptchaImpl;
import net.tanesha.recaptcha.ReCaptchaResponse;

import com.mongodb.BasicDBObject;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;

import data.Data;

@SuppressWarnings("serial")
public class RegisterServlet extends HttpServlet {

	public void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		Utils.checkLoginTrue(request, response);
		String[] listError = new String[6];
		request.setAttribute("listError", listError);
		try {
			request.getRequestDispatcher("/register.jsp").forward(request,
					response);
		} catch (Exception e) {
//			new SLogger(RegisterServlet.class.getName(), e.toString());
		}
	}

	public void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		Utils.checkLoginTrue(request, response);
		
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String domain = request.getParameter("domain");
		String usernamepostdomain = request.getParameter("usernamepostdomain");
		String passwordpostdomain = request.getParameter("passwordpostdomain");
		String email = request.getParameter("email");
		String passwordemail = request.getParameter("passwordemail");
		String blogid = request.getParameter("blogid");
		String type = request.getParameter("type");
		
		
		
		//fomat domain
		domain = Utils.fomatSitemap(domain);
		
		//valid form
		String[] listError = new String[6];
		if((username!=null && username.equals("")) || username==null)
			listError[0] = "Username is not empty";
		if((password!=null && password.equals("")) || password==null)
			listError[1] = "Password is not empty";
		
		//check capchar
		String remoteAddr = request.getRemoteAddr();
        ReCaptchaImpl reCaptcha = new ReCaptchaImpl();
        reCaptcha.setPrivateKey("6LcF-80SAAAAAM2kArjyUwtVJYbH_DKredY3Q26U");

        String challenge = request.getParameter("recaptcha_challenge_field");
        String uresponse = request.getParameter("recaptcha_response_field");
        if(challenge == null)
        {
        	challenge = "b";
        }
        if(uresponse == null)
        {
        	uresponse = "a";
        }
        ReCaptchaResponse reCaptchaResponse = reCaptcha.checkAnswer(remoteAddr, challenge, uresponse);

        if (!reCaptchaResponse.isValid()) {
        	listError[5] = "Invalid Capchar";
        }
        //create coleection
		DBCollection collection = Data.data("users");
		
		//check user 
		DBObject dbObject = collection.findOne(new BasicDBObject("username",username));
		if(dbObject!=null)
			listError[0] = "Username already exists !";
        
		if(listError[0]==null&&listError[1]==null&&listError[2]==null&&listError[3]==null&&listError[4]==null&&listError[5]==null)
		{
			
			//add user
			BasicDBObject basicDBObject = new BasicDBObject();
			if(type.equals("wordpress"))
			{
				basicDBObject.put("usernamepostdomain", usernamepostdomain);
				basicDBObject.put("passwordpostdomain", passwordpostdomain);
				basicDBObject.put("domain", domain);
			}
			if(type.equals("blogger"))
			{
				basicDBObject.put("email", email);
				basicDBObject.put("passwordemail", passwordemail);
				basicDBObject.put("blogid", blogid);
			}
			basicDBObject.put("username", username);
			basicDBObject.put("password", password);
			basicDBObject.put("type", type);
			
			collection.insert(basicDBObject);
			
			request.setAttribute("success", "Register success");
		}
		
		request.setAttribute("listError", listError);
		try {
			request.getRequestDispatcher("/register.jsp").forward(request,
					response);
		} catch (Exception e) {
//			new SLogger(RegisterServlet.class.getName(), e.toString());
		}
	}

}
